﻿Imports System.Data.SqlClient

Public Class frmAlmacenBoletos
    Dim Bitacora As New clsActividad
    Dim Usuario As New clsUsuarioPrincipal
    Private mdsDatos As DataSet
    Private mstrValor As String
    Private mstrCondicion As String
    Dim Kardex As New clsKardex
    Public ReadOnly Property Valor() As String
        Get
            Valor = mstrValor
        End Get
    End Property
    Public WriteOnly Property Condicion() As String
        Set(ByVal value As String)
            mstrCondicion = value
        End Set
    End Property

    Private Sub frmAlmacenBoletos_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Bitacora.RegistraActividad("Cerró el almacen de boletos")
        Usuario.Ubicacion("Modulo de Liquidación")

    End Sub
    Private Sub frmAlmacenBoletos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        dgNuevos.DefaultCellStyle.Font = New Font("Segoe UI", 10, FontStyle.Regular)

        Bitacora.RegistraActividad("Ingresó al almacen de boletos")
        Usuario.Ubicacion("Almacen de boletos")

        CargaBuses()
        Inicial()

        FormateaGrid()
    End Sub
    Sub FormateaGrid()
        dgNuevos.Font = New System.Drawing.Font("Arial", 8.0!, System.Drawing.FontStyle.Regular)

        dgNuevos.Columns(2).DefaultCellStyle.Format = "N0"
        dgNuevos.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        dgNuevos.Columns(3).DefaultCellStyle.Format = "N0"
        dgNuevos.Columns(3).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
        dgNuevos.Columns(4).DefaultCellStyle.Format = "N0"
        dgNuevos.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight

        dgNuevos.Columns(1).ReadOnly = True
        dgNuevos.Columns(3).ReadOnly = True
        dgNuevos.Columns(4).ReadOnly = True

    End Sub
    Sub Inicial()
        txtAutobus.Text = ""
        txtFinal.Text = 0
        txtInicial.Text = 0
        txtTotal.Text = 0
        txtSerie.Text = ""
        dgNuevos.Rows.Clear()

    End Sub
    Sub CargaBuses()
        Dim cnconn As New SqlConnection
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader
        Dim strSql As String
        Try
            cnconn.ConnectionString = CitraConnection
            strSql = "Select NoEconomico FROM Autobuses WHERE Estatus=1"

            cmdBuscar.Connection = cnconn
            cmdBuscar.CommandText = strSql

            cnconn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                txtAutobus.AutoCompleteCustomSource.Add(rdBuscar("NoEconomico").ToString.Trim)

            Loop
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try

    End Sub
    Private Sub txtInicial_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtInicial.KeyPress
        If IsNumeric(txtInicial.Text) Then
            If e.KeyChar = Chr(Keys.Enter) Then
                If txtInicial.Text = "" Then
                    txtInicial.Text = 0
                End If
                If txtFinal.Text < 0 Or txtFinal.Text <> "" Then
                    txtTotal.Text = (txtFinal.Text - txtInicial.Text) + 1
                End If
                txtFinal.Focus()

            End If
        Else
            MsgBox("La numeracion inicial es incorrecta", MsgBoxStyle.Exclamation, "Aviso")
        End If

    End Sub

    Private Sub txtInicial_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtInicial.Leave
        If IsNumeric(txtInicial.Text) Then
            If txtInicial.Text = "" Then
                txtInicial.Text = 0
            End If
            If txtFinal.Text < 0 Or txtFinal.Text <> "" Then
                txtTotal.Text = (txtFinal.Text - txtInicial.Text) + 1
            End If
        Else
            MsgBox("La numeracion inicial es incorrecta", MsgBoxStyle.Exclamation, "Aviso")
        End If

    End Sub

    Private Sub txtInicial_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtInicial.TextChanged

    End Sub

    Private Sub txtFinal_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtFinal.KeyPress
        If IsNumeric(txtFinal.Text) Then
            If e.KeyChar = Chr(Keys.Enter) Then
                If txtFinal.Text = "" Then
                    txtFinal.Text = 0

                End If
                txtTotal.Text = (txtFinal.Text - txtInicial.Text) + 1
                txtSerie.Focus()

            End If
        Else
            MsgBox("La numeracion final es incorrecta", MsgBoxStyle.Exclamation, "Aviso")
        End If

    End Sub

    Private Sub txtFinal_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFinal.Leave
        If IsNumeric(txtFinal.Text) Then
            If txtFinal.Text = "" Then
                txtFinal.Text = 0

            End If
            txtTotal.Text = (txtFinal.Text - txtInicial.Text) + 1
        Else
            MsgBox("La numeracion final es incorrecta", MsgBoxStyle.Exclamation, "Aviso")

        End If

    End Sub

    Private Sub txtFinal_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFinal.TextChanged

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


    End Sub


    Private Sub txtAutobus_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAutobus.KeyPress
        If e.KeyChar = Chr(Keys.Enter) Then
            txtInicial.Focus()
        End If
    End Sub

    Private Sub txtAutobus_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtAutobus.Leave
        BuscaProductos()

    End Sub

    Private Sub txtAutobus_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAutobus.TextChanged

    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Inicial()





    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click

        Me.Close()

    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        Inicial()


    End Sub

    Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
    Sub BuscaProductos()
        dgNuevos.Rows.Clear()

        Dim cnconn As New SqlConnection
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Dim cidClien01 As Integer
        Dim strSql As String = ""
        Dim TOTAL As Double

        Try
            cnconn.ConnectionString = CitraConnection

            cidClien01 = BuscarIntDatoCitra("Autobuses", "IdAutobus", "NoEconomico='" & txtAutobus.Text.Trim & "'")

            strSql = "Select * FROM KARDEX WHERE IdAutobus=" & cidClien01 & " ORDER BY IDBOLETO"

            cmdBuscar.Connection = cnconn
            cmdBuscar.CommandText = strSql

            cnconn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                TOTAL = (rdBuscar("Final") - rdBuscar("Inicial")) + 1
                If TOTAL > 1 Then
                    dgNuevos.Rows.Add(rdBuscar("IdBoleto"), rdBuscar("Serie").ToString.Trim, rdBuscar("Inicial"), rdBuscar("Final"), CDbl(rdBuscar("Final")) - CDbl(rdBuscar("Inicial")) + 1)
                End If
            Loop

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")

        Finally
            If cnconn.State = ConnectionState.Open Then
                cnconn.Close()
            End If
        End Try

    End Sub
    Private Sub RadButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton1.Click
        'Empieza del ultimo folio hacia el primero
        If txtInicial.Text = "" Then
            MsgBox("Debes indicar la numeracion inicial", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If
        If txtFinal.Text = "" Then
            MsgBox("Debes indicar la numeracion final", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If
        If txtSerie.Text = "" Then
            MsgBox("Debes indicar la serie", MsgBoxStyle.Exclamation, "Aviso")
            Exit Sub
        End If
        Kardex.IdAutobus = BuscarIntDatoCitra("Autobuses", "IdAutobus", "NoEconomico='" & txtAutobus.Text.Trim & "'")
        Kardex.Serie = UCase(txtSerie.Text.Trim)
        Kardex.Inicial = txtInicial.Text
        Kardex.Final = txtFinal.Text
        Kardex.TotalBoletos = txtTotal.Text
        Kardex.Insertar()

        Bitacora.RegistraActividad("Registró la numeracion de boletos del " & txtInicial.Text & " al " & txtFinal.Text & " del autobus " & txtAutobus.Text.Trim)

        txtFinal.Text = 0
        txtInicial.Text = 0
        txtTotal.Text = 0
        txtSerie.Clear()
    End Sub

    Private Sub txtSerie_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSerie.KeyPress
        If e.KeyChar = Chr(Keys.Enter) Then
            RadButton1.Focus()
        End If
    End Sub

    Private Sub txtSerie_Leave(sender As Object, e As System.EventArgs) Handles txtSerie.Leave

        If txtFinal.Text = "" Then
            txtFinal.Text = 0

        End If
        txtTotal.Text = (txtFinal.Text - txtInicial.Text) + 1
    End Sub

    Private Sub txtSerie_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSerie.TextChanged

    End Sub

    Private Sub ToolStripButton3_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolStripButton3.LocationChanged

    End Sub

    Private Sub ExistenciasDeBoletosToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExistenciasDeBoletosToolStripMenuItem.Click


    End Sub

    Private Sub BorrarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles BorrarToolStripMenuItem.Click
        Dim CidBoleto As Integer = dgNuevos.CurrentRow.Cells(0).Value
        Kardex.IdBoleto = CidBoleto
        Kardex.Borrar()

        Bitacora.RegistraActividad("Eliminó la numeracion de boletos del " & dgNuevos.CurrentRow.Cells(2).Value & " al " & dgNuevos.CurrentRow.Cells(3).Value & " del autobus " & txtAutobus.Text.Trim)
        BuscaProductos()
    End Sub
    Private Sub FinDeEdicion(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles dgNuevos.CellEndEdit
        'Añadir aqui las validaciones necesarias
        Try
            dgNuevos.Rows(e.RowIndex).Cells(4).Value = (dgNuevos.Rows(e.RowIndex).Cells(3).Value - dgNuevos.Rows(e.RowIndex).Cells(2).Value) + 1
            Kardex.IdBoleto = dgNuevos.Rows(e.RowIndex).Cells(0).Value
            Kardex.Serie = dgNuevos.Rows(e.RowIndex).Cells(1).Value
            Kardex.Inicial = dgNuevos.Rows(e.RowIndex).Cells(2).Value
            Kardex.Final = dgNuevos.Rows(e.RowIndex).Cells(3).Value
            Kardex.TotalBoletos = dgNuevos.Rows(e.RowIndex).Cells(4).Value
            Kardex.Actualizar()

        Catch ex As Exception
            '.... Accion en caso de excepcion
        End Try
    End Sub

    Private Sub ToolStripButton4_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton4.Click
        Dim cidclien01 As Integer = BuscarIntDatoCitra("Autobuses", "IdAutobus", "NoEconomico='" & txtAutobus.Text.Trim & "'")
        Call SetSqlCitra("Delete FROM Kardex WHERE IdAutobus=" & cidclien01)
        BuscaProductos()
    End Sub
End Class